package com.yilin.tms.system.service.impl;

import com.yilin.commons.util.DateTimeUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.components.repository.OperateLogRepository;
import com.yilin.tms.core.entity.system.handle.OperateLog;
import com.yilin.tms.system.service.IOperateLogService;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class OperateLogService implements IOperateLogService {

    @Resource
    private OperateLogRepository operateLogRepository;


    @Override
    public PageData<OperateLog> getOperateLogPage(Integer page, Integer limit) {
        Sort sort = Sort.by(Sort.Direction.DESC, "optTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Page<OperateLog> logPage = operateLogRepository.findAll(pageable);
        return new PageData<>(logPage.getTotalElements(), logPage.toList());
    }

    @Override
    public void deleteOperateLogByIds(List<String> operateLogIds) {
        operateLogRepository.deleteByIdIn(operateLogIds);
    }

    @Override
    public void deleteOperateLogByTimeRange(Date startTime, Date endTime) {
        operateLogRepository.deleteByOptTimeBetween(DateTimeUtil.plusHourDate(startTime, 480),
                DateTimeUtil.plusHourDate(endTime, 480));
    }
}
